package com.inputabc.ct.v1.ui.action;

import com.inputabc.ct.v1.context.ContextParams;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.Map;

public class TextBoxAction {
    private Map<Object, Object> contextParam = ContextParams.contextParam;

    public void left_top(MouseEvent me, JFrame jf) {

        int mouseX = me.getXOnScreen();
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = jfX - mouseX;
        int offsetY = jfY - mouseY;
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getWidth() == jf.getWidth() || jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preX", jfX);
            contextParam.put("preY", jfY);
        }
        jf.setLocation(mouseX, mouseY);
    }

    public void right_top(MouseEvent me, JFrame jf) {
        int mouseX = me.getXOnScreen();
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = mouseX - (jfX + jf.getWidth());
        int offsetY = jfY - mouseY;
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getWidth() == jf.getWidth() || jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preX", jfX);
            contextParam.put("preY", jfY);
        }
        jf.setLocation(jfX, mouseY);
    }

    public void left_bottom(MouseEvent me, JFrame jf) {
        int mouseX = me.getXOnScreen();
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = jfX - mouseX;
        int offsetY = mouseY - (jfY + jf.getHeight());
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getWidth() == jf.getWidth() || jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preX", jfX);
            contextParam.put("preY", jfY);
        }
        jf.setLocation(mouseX, jfY);
    }

    public void right_bottom(MouseEvent me, JFrame jf) {
        int mouseX = me.getXOnScreen();
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = mouseX - (jfX + jf.getWidth());
        int offsetY = mouseY - (jfY + jf.getHeight());
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getWidth() == jf.getWidth() || jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preX", jfX);
            contextParam.put("preY", jfY);
        }
        jf.setLocation(jfX, jfY);
    }

    public void left(MouseEvent me, JFrame jf) {
        int mouseX = me.getXOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = jfX - mouseX;
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight());
        if (jf.getMinimumSize().getWidth() == jf.getWidth()) {
            contextParam.put("preX", jfX);
        }
        jf.setLocation(jfX - offsetX, jfY);
    }

    public void right(MouseEvent me, JFrame jf) {
        int mouseX = me.getXOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetX = mouseX - (jfX + jf.getWidth());
        jf.setSize(jf.getWidth() + offsetX, jf.getHeight());
        if (jf.getMinimumSize().getWidth() == jf.getWidth()) {
            contextParam.put("preX", jfX);
        }
        jf.setLocation(jfX, jfY);
    }

    public void top(MouseEvent me, JFrame jf) {
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetY = jfY - mouseY;
        jf.setSize(jf.getWidth(), jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preY", jfY);
        }
        jf.setLocation(jfX, mouseY);
    }

    public void bottom(MouseEvent me, JFrame jf) {
        int mouseY = me.getYOnScreen();
        int jfX = jf.getX();
        int jfY = jf.getY();
        int offsetY = mouseY - (jfY + jf.getHeight());
        jf.setSize(jf.getWidth(), jf.getHeight() + offsetY);
        if (jf.getMinimumSize().getHeight() == jf.getHeight()) {
            contextParam.put("preY", jfY);
        }
        jf.setLocation(jfX, jfY);
    }
}
